import { createRouter, createWebHistory } from 'vue-router'
import MainLayout from '../layouts/MainLayout.vue'
import DocumentUpload from '../views/DocumentUpload.vue'
import KnowledgeBaseManagement from '../views/KnowledgeBaseManagement.vue'
import MemberCenter from '../views/MemberCenter.vue'
import NotificationSettings from '../views/NotificationSettings.vue'
import Order from '../views/Order.vue'
import PointsManagement from '../views/PointsManagement.vue'
import SecuritySettings from '../views/SecuritySettings.vue'
import User from '../views/User.vue'
import AiChat from '../views/AiChat.vue'
import AiDrawing from '../views/AiDrawing.vue'
import ProductDisplay from '@/views/ProductDisplay.vue'
import AiVideo from '../views/AiVideo.vue'
import Login from '../views/Login.vue'
import Pay from '../views/Pay.vue'
import PayCode from '../views/PayCode.vue'
import PaySuccess from '../views/PaySuccess.vue'
import ForgotPassword from '../views/ForgotPassword.vue'
import { getToken } from '@/utils/auth'
import Clock from '../views/Clock.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: { requiresAuth: false },
    },
    {
      path: '/forgot-password',
      name: 'ForgotPassword',
      component: ForgotPassword,
      meta: { requiresAuth: false },
    },
    {
      path: '/',
      component: MainLayout,
      meta: { requiresAuth: true },
      children: [
        {
          path: '',
          name: 'User',
          component: User,
        },
        {
          path: 'document-upload',
          name: 'DocumentUpload',
          component: DocumentUpload,
        },
        {
          path: 'knowledge-base-management',
          name: 'KnowledgeBaseManagement',
          component: KnowledgeBaseManagement,
        },
        {
          path: 'member-center',
          name: 'MemberCenter',
          component: MemberCenter,
        },
        {
          path: 'notification-settings',
          name: 'NotificationSettings',
          component: NotificationSettings,
        },
        {
          path: 'order',
          name: 'Order',
          component: Order,
        },
        {
          path: 'points-management',
          name: 'PointsManagement',
          component: PointsManagement,
        },
        {
          path: 'security-settings',
          name: 'SecuritySettings',
          component: SecuritySettings,
        },
        {
          path: 'ai-chat',
          name: 'AiChat',
          component: AiChat,
        },
        {
          path: 'ai-drawing',
          name: 'AiDrawing',
          component: AiDrawing,
        },
        {
          path: 'ai-video',
          name: 'AiVideo',
          component: AiVideo,
        },
        {
          path: 'product-display',
          name: 'ProductDisplay',
          component: ProductDisplay,
        },{
          path: '/pay',
          name: 'Pay',
          component: Pay,
        },
        {
          path: 'user-clock',
          name: 'UserClock',
          component: Clock,
        },
        {
          path: '/pay-code',
          name: 'PayCode',
          component: PayCode,
        },
        {
          path: '/pay-success',
          name: 'PaySuccess',
          component: PaySuccess,
        }
      ],
    },
  ],
})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  const requiresAuth = to.matched.some((record) => record.meta.requiresAuth !== false)
  const hasToken = !!getToken()

  if (requiresAuth && !hasToken) {
    // 需要登录但没有token，重定向到登录页
    next('/login')
  } else if ((to.path === '/login' || to.path === '/forgot-password') && hasToken) {
    // 已登录但访问登录页或忘记密码页，重定向到首页
    next('/')
  } else {
    next()
  }
})

export default router
